﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using DiscountSharing.FacebookContracts;

namespace DiscountSharing.LocalModels
{
    public class Post
    {
        public string ID;
        public string Place;
        public string Message;
        public string DiscountPic;

        public int LikeCount;
        public int CommentCount;

        public Friend Writer;
        public DateTime UpdatedTime;
        public Category DiscountType;
        public GeoCoordinate Location;

        public Post(PostContract p)
        {
            ID = p.post_id;

            foreach (Friend friend in ((App)App.Current).Friends)
            {
                if (friend.ID == p.source_id)
                {
                    Writer = friend;
                    break;
                }
            }
            if (Writer == null)
            {
                throw new Exception("Incorrect friend!");
            }

            try
            {
                Message = p.message.Split('@')[0];
                string[] tmp = p.message.Split(':');
                Place = tmp[tmp.Length - 1];
                tmp = p.message.Split('@');
                string type = tmp[1].Split(':')[0];
                switch (type)
                {
                    case "Food":
                        DiscountType = Category.Food;
                        break;
                    case "Clothes":
                        DiscountType = Category.Clothes;
                        break;
                    case "Electronics":
                        DiscountType = Category.Electronics;
                        break;
                    case "Entertainment":
                        DiscountType = Category.Entertainment;
                        break;
                    case "Services":
                        DiscountType = Category.Services;
                        break;
                    default:
                        DiscountType = Category.Others;
                        break;
                }
                UpdatedTime = UnixTimeStampToDateTime(Convert.ToDouble(p.updated_time));
            }
            catch
            {
                throw new Exception("Incorrect format!");
            }
        }

        public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            // Unix timestamp is seconds past epoch
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }

    }
}
